#include "mainscene.h"
#include"config.h"
#include <QIcon>
#include<qpainter.h>
#include<QPixmap>
#include<QPainter>
#include <ctime>
#include<QMessageBox>
#include"QString"
#include<enemyplane.h>
MainScene::MainScene(QWidget *parent): QWidget(parent)
{

    initScene();
  playGame();


}
void MainScene::initScene()
{
    //初始化窗口大小
   setFixedSize(GAME_WIDTH,GAME_HEIGHT);
   setWindowIcon(QIcon( GAME_ICON));
    //设置窗口标题
    setWindowTitle(GAME_TITLE);
 m_Timer.setInterval(GAME_RATE);
 m_recorder=0;
 srand((unsigned int)time(NULL));
}
 void MainScene::enemyToScene()
 {
     m_recorder++;
     if(m_recorder < ENEMY_INTERVAL)
     {
         return;
     }
     m_recorder = 0;
 for(int i = 0 ; i< ENEMY_NUM;i++)
 {
     if(m_enemys[i].m_Free)
     {
         //敌机空闲状态改为false
         m_enemys[i].m_Free = false;
         //设置坐标
         m_enemys[i].m_X = rand() % (GAME_WIDTH - m_enemys[i].m_Rect.width());
         m_enemys[i].m_Y = -m_enemys[i].m_Rect.height();
         break;
     }
   }
 }

void MainScene::playGame()
{
    //启动定时器
   m_Timer.start();

    //监听定时器
    connect(&m_Timer,&QTimer::timeout,[=](){
        //更新游戏中元素的坐标

        enemyToScene();
        updatePosition();
        //重新绘制图片
        collisionDetection();
        update();
    });
}

void MainScene::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();
    //发射子弹
      m_hero.shoot();
     for(int i = 0 ; i< ENEMY_NUM;i++) {m_enemys[i].eshoot();}
    //计算子弹坐标
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_hero.m_bullets[i].m_Free)
        {
            m_hero.m_bullets[i].updatePosition();
        }
    }
    //敌机坐标计算
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        //非空闲敌机 更新坐标
        if(m_enemys[i].m_Free == false)
        {
            m_enemys[i].updatePosition();
        }
    }
    for(int i = 0 ; i< ENEMY_NUM;i++){

    for(int j = 0 ;j <ENEMY_BULLET_NUM;j++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(!m_enemys[i].m_enemybullets[j].m_Free&&m_enemys[i].m_Free == false)
        {
            m_enemys[i].m_enemybullets[j].updatePosition();
        }
    }
    }
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
        {
                 m_bombs[i].updateInfo();
        }
    }
if(life==0) {m_Timer.stop();}
     update();
}

void MainScene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY , m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY , m_map.m_map2);
    QFont font2("Courier",15);
       painter.setFont(font2);
       painter.setPen(Qt::blue);
       painter.setBrush(Qt::blue);
       painter.drawText(0,500,QString::number(score));
       painter.setPen(Qt::red);
       painter.setBrush(Qt::red);
       painter.drawText(0,800,QString::number(life));
       if(life==0){
            QPainter painter(this);
           painter.setFont(font2);
           painter.setPen(Qt::blue);
           painter.setBrush(Qt::blue);
           painter.drawText(500,500,QString::number(score));
           painter.setPen(Qt::red);
           painter.setBrush(Qt::red);
           painter.drawText(0,800,"Gameover");

       }
    //绘制玩家
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
    //绘制子弹
    for(int i = 0 ;i < BULLET_NUM;i++)
    {
          //如果子弹状态为非空闲，计算发射位置
          if(!m_hero.m_bullets[i].m_Free)
          {
                    painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
           }
    }
    //绘制敌机
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }
    for(int i = 0 ; i< ENEMY_NUM;i++)
    {
    for(int j = 0 ;j <ENEMY_BULLET_NUM;j++)
    {
        //如果子弹状态为非空闲，计算发射位置
        if(m_enemys[i].m_enemybullets[j].m_Free==false&&m_enemys[i].m_Free == false)
        {
            painter.drawPixmap(m_enemys[i].m_enemybullets[j].m_X,m_enemys[i].m_enemybullets[j].m_Y,m_enemys[i].m_enemybullets[j].m_enemybullet) ;
        }
    }
    }
    for(int i = 0 ; i < BOMB_NUM;i++)
    {
        if(m_bombs[i].m_Free == false)
       {        painter.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);

        }
    }

}

void MainScene::keyPressEvent(QKeyEvent *event) // 飞机随之移动
{   int x,y;
    QKeyEvent *key=(QKeyEvent*)event;
        switch (key->key()){
       case Qt::Key_Up:y=m_hero.m_Y-m_hero.m_Rect.height()*0.3;
            x=m_hero.m_X;
           break;
       case Qt::Key_Down:y=m_hero.m_Y+m_hero.m_Rect.height()*0.3;
            x=m_hero.m_X;
           break;
       case Qt::Key_Left:x=m_hero.m_X-m_hero.m_Rect.width()*0.3;
             y=m_hero.m_Y;
           break;
       case Qt::Key_Right:x=m_hero.m_X+m_hero.m_Rect.width()*0.3;
            y=m_hero.m_Y;
           break;

       default:;
       }

    if(x<=0)
    {
        x=0;
    }
    if(x>GAME_WIDTH-m_hero.m_Rect.width())
    {
        x=GAME_WIDTH-m_hero.m_Rect.width();
    }
    if(y<=0)
    {
        y=0;
    }
    if(y>=GAME_HEIGHT-m_hero.m_Rect.height())
    {
        y=GAME_HEIGHT-m_hero.m_Rect.height();
    }
m_hero.setPosition(x,y);

update();
}
void MainScene::collisionDetection()
{

    //遍历所有非空闲的敌机
    for(int i = 0 ;i < ENEMY_NUM;i++)
    {
        for(int j = 0 ; j <ENEMY_BULLET_NUM;j++)
    {


        if(m_enemys[i].m_enemybullets[j].m_Rect.intersects(m_hero.m_Rect)&& m_enemys[i].m_enemybullets[j].m_Free==false )
        { life-=0.1;
            m_enemys[i].m_enemybullets[j].m_Free = true;
 m_enemys[i].m_enemybullets[j].updatePosition();
                }
            }
}
        for(int i = 0 ;i < ENEMY_NUM;i++)
        {
        if(m_enemys[i].m_Free)
        {
            //空闲飞机 跳转下一次循环
            continue;
        }
        //遍历所有 非空闲的子弹
        for(int j = 0 ; j < BULLET_NUM;j++)
    {
        if(m_hero.m_bullets[j].m_Free)
        {
            //空闲子弹 跳转下一次循环
            continue;
        }
        //如果子弹矩形框和敌机矩形框相交，发生碰撞，同时变为空闲状态即可
        if(m_enemys[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect))
        {    score+=20;
            m_enemys[i].m_Free = true;
            m_hero.m_bullets[j].m_Free = true;
            for(int k = 0 ; k < BOMB_NUM;k++) { if(m_bombs[k].m_Free) {
                    m_bombs[k].m_Free = false;
                    m_bombs[k].m_X = m_enemys[i].m_X;
                    m_bombs[k].m_Y = m_enemys[i].m_Y;
                    break;
                }
            }
        }
    }

}
    }



MainScene::~MainScene()
{

}
